package config

import (
	"github.com/kaylee595/tracerr"
	"github.com/spf13/viper"
)

type Config struct {
	Username    string
	Password    string
	RROcrAppkey string `mapstructure:"rrocr_appkey"`
}

var Cfg = &Config{}

func LoadConfig(filename string) (*Config, error) {
	if len(filename) > 0 {
		viper.SetConfigFile(filename)
	} else {
		viper.AddConfigPath(".")
		viper.SetConfigName("config")
		viper.SetConfigType("yaml")
	}
	err := viper.ReadInConfig()
	if err == nil {
		result := new(Config)
		err = viper.Unmarshal(result)
		if err != nil {
			return nil, tracerr.Wrap(err)
		}
		return result, nil
	}
	return Cfg, nil
}
